// pages/other/other.js
const app = getApp();
const db = wx.cloud.database();
const _ = db.command;
const $ = db.command.aggregate
Page({

  /**
   * 页面的初始数据
   */
  data: {
    myinfo: null

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let userid = options.userid;
    if(!userid){
      wx.switchTab({
        url: '../dongtai/dongtai',
      })
    }
    this.getInfo(userid);
  },

  onShow() {

  },

  getInfo(id) {
    db.collection('user').aggregate().match({
      _id: id,
      history: _.exists(true)
    }).project({
      jonnerName: 1,
      jf: 1,
      image: '$userWxInfo.avatarUrl',
      gender: '$userWxInfo.gender',
      city: '$userWxInfo.city',
      gametimes: $.size('$history'),
      avg: $.divide([$.sum('$history'), $.size('$history')]),
      firstcount: $.size($.filter({
        input: '$history',
        as: 'item',
        cond: $.eq(['$$item', 1])
      })),
      secondcount: $.size($.filter({
        input: '$history',
        as: 'item',
        cond: $.eq(['$$item', 2])
      })),
      thirdcount: $.size($.filter({
        input: '$history',
        as: 'item',
        cond: $.eq(['$$item', 3])
      })),
      lastcount: $.size($.filter({
        input: '$history',
        as: 'item',
        cond: $.eq(['$$item', 8])
      }))
    }).end().then(res => {
      if (res.list.length == 1) {
        let myinfo=res.list[0]
        wx.setNavigationBarTitle({
          title: `《${myinfo.jonnerName}》战绩`,
        })
        this.setData({
          myinfo
        })
      }
    })
  },

  tozw() {
    wx.navigateTo({
      url: '../history/history',
    })
  }
})
